home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5105 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.5 KB  |  64 lines

  1. Path: news2.ios.com!usenet
  2. From: vlad@gramercy.ios.com (Vlastimil Adamovsky)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Is it possible to use templates with structures?
  5. Date: Fri, 02 Feb 1996 15:09:00 GMT
  6. Organization: Internet Online Services
  7. Message-ID: <4et8su$35t@news2.ios.com>
  8. References: <4eq9s5$l14@nntp.hut.fi>
  9. NNTP-Posting-Host: ppp-43.ts-7.hck.idt.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. casper@lk-hp-10.hut.fi (Casper Gripenberg) wrote:
  13.  
  14.  
  15.  
  16. >Can I do:
  17.  
  18. >template<class T>
  19. >struct Foo {
  20. >  T *data;
  21. >};
  22.  
  23. >??
  24.  
  25. >The compiler doesn't seem to complain but I would still like to be
  26. >sure if it's ok to do it. 
  27.  
  28. >The reason I want to do it is that I need to allocate the structure
  29. >with my own allocation routine and not with new. Also I can't override
  30. >new in the class so that won't work either. While on the same subject
  31. >could I have a template class dontaining just data members and
  32. >no constructors/destructor or memberfunctions and then allocate
  33. >it without using new? As I understand it the only thing new does
  34. >that for example malloc doesn't do is run the constructor code. So
  35. >is it safe to use that kind of a template class allocated with malloc??
  36.  
  37. >Thanks...
  38.  
  39. I don't see any problem here since your code 
  40.  
  41.   template<class T>
  42.  struct Foo {
  43.   T *data;
  44.  };
  45.  
  46. is understood by your compiler as a 
  47.  
  48.  template<class T>
  49.  class Foo
  50.  {
  51.    public:
  52.        T *data;
  53.  };
  54.  
  55. and the latter is correct.
  56.  
  57.  
  58.  
  59. *******************************************
  60. *    Vlastimil Adamovsky                  *
  61. * Smalltalk, C++ and Envelop development  *
  62. *******************************************
  63.  
  64.